package com.monkey.luffer.springboot.demo.basis.kcc.test.lianji.majiang;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author jiahong.liu
 */
public class Majiang {
    private final List<Card> cards = new ArrayList<>();
    static String [] zi = {"红中", "发财", "白板", "东风", "南风", "西风", "北风"};
    public Majiang() {
        for (Card.Suit suit: Card.Suit.values()) {
            if (suit == Card.Suit.TIAO || suit == Card.Suit.TONG || suit == Card.Suit.WAN) {
                for (int i = 1; i <= 9; i++) {
                    for (int j = 0 ; j < 4; j++) {
                        cards.add(new Card(i, suit));
                    }
                }
            } else {
                for (int j = 0 ; j < 4; j++) {
                    cards.add(new Card(1, suit));
                }
            }

        }
    }

    public void shuffle() {
        Collections.shuffle(cards);
    }

    public Card dealCard() {
        if (cards.isEmpty()) {
            throw new IllegalStateException("Deck is empty, cannot deal cards.");
        }
        return cards.remove(0);
    }

    public boolean isEnd() {
        return cards.isEmpty();
    }



    @Data
   public static class Card {

        public static final String[][] CARD_FACE = {
                {"\uD83C\uDC07","\uD83C\uDC08","\uD83C\uDC09","\uD83C\uDC0A",
                        "\uD83C\uDC0B","\uD83C\uDC0C","\uD83C\uDC0D","\uD83C\uDC0E","\uD83C\uDC0F"
                },
                {"\uD83C\uDC10","\uD83C\uDC11","\uD83C\uDC12","\uD83C\uDC13",
                        "\uD83C\uDC14","\uD83C\uDC15","\uD83C\uDC16","\uD83C\uDC17","\uD83C\uDC18"
                },
                {"\uD83C\uDC19","\uD83C\uDC1A","\uD83C\uDC1B","\uD83C\uDC1C",
                        "\uD83C\uDC1D","\uD83C\uDC1E","\uD83C\uDC1F","\uD83C\uDC20","\uD83C\uDC21"
                },
                {
                    "\ud83c\udc04","","","", "", "", "", "", ""
                },
                {
                        "\ud83c\udc05","","","", "", "", "", "", ""
                },
                {
                        "\ud83c\udc06","","","", "", "", "", "", ""
                },
                {
                        "\ud83c\udc00","","","", "", "", "", "", ""
                },
                {
                        "\ud83c\udc01","","","", "", "", "", "", ""
                },
                {
                        "\ud83c\udc02","","","", "", "", "", "", ""
                },
                {
                        "\ud83c\udc03","","","", "", "", "", "", ""
                }
        };


        private int number;
        private Suit suit;

        public Card(int number, Suit suit) {
            this.number = number;
            this.suit = suit;
        }


        public static Card getCard(Integer card) {
            int suffix = card / 10;
            Suit suit = Suit.getSuitBySuffix(suffix);
            return new Card(card % suit.getSalt(), suit);
        }

        public static Card getCard(String cardName) {
            String [] card = cardName.split("");
            if (card.length != 2) {
                return null;
            }

            if (!isNum(card[0])) {
                // 考虑是字牌, 用字的数组进行匹配
                Suit suit = Suit.getSuitByValue(cardName);
                return new Card( 1, suit);
            }

            Suit suit = Suit.getSuitByValue(card[1]);
            if (suit == null) {
                return null;
            }

            return new Card(Integer.parseInt(card[0]), suit);
        }


        public static boolean isNum(String num) {
            try {
                int n = Integer.parseInt(num);
                if (n < 0 || n > 9) {
                    return false;
                }
            }catch (Exception e) {
                return false;
            }
            return true;
        }

        public String printCard() {
//            return this.getNumber() + this.getSuit().getValue();
            return CARD_FACE[this.getSuit().getSuffix() - 1][this.getNumber() - 1];
        }

        public String printCard(int num) {
            String card = "";
            for (int i = 0; i < num; i++) {
                card += CARD_FACE[this.getSuit().getSuffix() - 1][this.getNumber() - 1];
            }

            return card;
        }



       @AllArgsConstructor
       @Getter
       public enum Suit {
           WAN("万", 10, 1),
           TIAO("条", 20, 2),
           TONG("筒",30, 3),
           ZH("红中", 40, 4),
           FA("发财", 50, 5),
           BAI("白板", 60, 6),
           DONG("东风", 70, 7),
           NAN("南风", 80, 8),
           XI("西风", 90, 9),
           BEI("北风", 100, 10),;
           String value;
           Integer salt;
           int suffix;

           public static Suit getSuitBySuffix(int suffix) {
               for (Suit suit: Suit.values()) {
                   if (suffix == suit.getSuffix()) {
                       return suit;
                   }
               }
               return null;
           }

           public static Suit getSuitByValue(String value) {
               for (Suit suit: Suit.values()) {
                   if (suit.getValue().equals(value)) {
                       return suit;
                   }
               }
               return null;
           }
       }
   }
}
